package com.vancouver.knows.ui.activity.search;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vancouver.knows.R;

import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Chris Li on 2016-07-12.
 */
public class SearchListViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static int TAG_TYPE = 0;
    public static int HISTORY_TYPE = 1;

    private int mAdapterType;
    private List<String> mItems;

    private OnSearchItemClickListener mListener;

    public SearchListViewAdapter(int type, List<String> items) {
        this.mAdapterType = type;
        this.mItems = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (mAdapterType == TAG_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tag_view_item, parent, false);
            return new SearchListViewHolder(view);
        } else if (mAdapterType == HISTORY_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_view_item, parent, false);
            return new SearchListViewHolder(view);
        }

        throw new RuntimeException("No matching view type.");
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof SearchListViewHolder) {
            SearchListViewHolder searchListViewHolder = (SearchListViewHolder) holder;
            searchListViewHolder.text.setText(mItems.get(position));

            searchListViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onSearchItemClick(mItems.get(holder.getAdapterPosition()));
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {

        if (mAdapterType == TAG_TYPE) {
            if (mItems.size() > 12) {
                return 12;
            } else {
                return mItems.size();
            }
        } else {
            return mItems.size();
        }
    }

    public void addItems(List<String> tags) {
        mItems.addAll(tags);
        notifyDataSetChanged();
    }

    public void addItem(String item) {
        if (!mItems.contains(item)) {
            mItems.add(0, item);
            notifyDataSetChanged();
        }
    }

    public void clearItems() {
        mItems.clear();
        notifyDataSetChanged();
    }

    public void reorderItems() {
        Collections.shuffle(mItems);
        notifyDataSetChanged();
    }

    public void setOnSearchItemClickListener(OnSearchItemClickListener listener) {
        this.mListener = listener;
    }

    public static class SearchListViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tag)
        TextView text;

        public SearchListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnSearchItemClickListener {
        void onSearchItemClick(String searchTerm);
    }

}
